<?php 

class LIST_UPAF {
	var $frow;
	var $srow;
	var $links;
	var $jump;
	var $adsq;
	var $sql;
	var $main;
	var $lines;
	var $mark_o;
	var $no_u;
	
	function LIST_UPAF($main, $sql, $frow, $srow=array(), $links=array(), $jump=array(), $lines="ALL", $adsq="", $mark_o=false, $no_u=false){
		$this->sql=$sql;
		$this->main=$main;
		$this->links=$links;
		$this->adsq=$adsq;
		$this->jump=$jump;
		$this->frow=$frow;
		$this->srow=$srow;
		$this->lines=$lines;
		$this->mark_o=$mark_o;
		$this->no_u=$no_u;
	}
	
	function generateList(){
		//echo "test";
		
		$array_key=array();
		$sql="SELECT 
			us.user_id user_id_s,
			al.ally_id ally_id_s,
			pl.planet_id planet_id_s";
		foreach(array_unique(array_merge($this->frow, $this->srow)) as $key=>$value){
			if(preg_match("! AS !", $value)){
				$sql.="\n, $value";
				$array_key[]=$key;
			}else
				$sql.="\n, ".substr($value,0,2).".$value";
		}
		$sql.="	FROM 
			".$this->sql->getpre()."users us
			LEFT JOIN ".$this->sql->getpre()."planet pl ON pl.user_id=us.user_id
			LEFT JOIN ".$this->sql->getpre()."ally al ON al.ally_id=us.user_ally
			LEFT JOIN ".$this->sql->getpre()."fleet fl ON fl.user_id=us.user_id
			LEFT JOIN ".$this->sql->getpre()."forschung fo ON fo.user_id=us.user_id
		";
		$sql_="";
		if(is_numeric($this->lines))
			$sql_="LIMIT ".$this->lines;
		elseif($this->lines=="ALL")
			$sql_="";
		else
			$sql.=$this->lines;
		
		
		$sql.=" ".$this->adsq." ";
		if(isset($_GET['sort']) && in_array($_GET['sort'], $this->frow)){
			$sql.="ORDER BY ".substr($_GET['sort'],0,2).".".$_GET['sort'];
			if(isset($_GET['desc']))
				$sql.=" DESC";
			$sql.="\n";
		}elseif(isset($_GET['sort']) && in_array($_GET['sort'], $array_key)){
			$sql.="ORDER BY ".$_GET['sort'];
			if(isset($_GET['desc']))
				$sql.=" DESC";
			$sql.="\n";
		}else
			$sql.="ORDER BY pl.planet_kords\n";	
		
		$sql.=$sql_;
		
		$this->sql->query($sql);
		
		$this->table_start();
		$count=1;
		while($this->sql->fetch_assoc()){
			$count++;
			$gala=10000;
			if(!isset($this->sql->row['planet_kords']) && $count%2)
				$gala=0;
			elseif(isset($this->sql->row['planet_kords']))
				$gala=substr($this->sql->row['planet_kords'],0,1);
			
			$user=0;
			$this->built_frow($gala, $this->sql->row['user_id_s'], $count);
			$this->built_srow($count);
			
			//print_r($this->sql->row);
		}
		$this->table_end();
	}
	
	function table_start(){
		echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n\t<tr>\n";
		
		$s="";
		$w=0;
		if(isset($_GET['sort'])){
			$s=$_GET['sort'];
			unset($_GET['sort']);
		}
		if(isset($_GET['desc'])){
			$w=1;
			unset($_GET['desc']);
		}	
			
		foreach($this->frow as $key=>$value){
			$pp=ceil(100/sizeof($this->frow));
			if(substr($key,0,1)!="<" && substr($key,1,2)!="<"){
				echo "\t\t<th width=\"$pp%\">";
				if(preg_match("! AS !", $value))
					echo "<a href=\"".fullURL()."&sort=$key";
				else
					echo "<a href=\"".fullURL()."&sort=$value";
				
				if(($s==$value || $s==$key) && $w==0)
					echo "&desc";
				echo "\">$key</a>";
			}else{
				echo "\t\t<th width=\"20\">".$key;
			}
			echo "</th>\n";
		}
		if(sizeof($this->links)>0 or sizeof($this->srow)>0)
			echo "\t\t<th width=\"80\">&nbsp;</th>\n";
		echo "\t</tr>\n";
		if($w==1)
			$_GET['desc']=true;
		$_GET['sort']=$s;
	}
	
	function table_end(){
		echo "</table>\n";
	}
	
	function built_frow($gala,$user, $count){
		if($this->mark_o && $user==$this->main->user_id)
			echo "<tr style=\"background-image:url(images/map_04.png); filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/map_04.png', sizingMethod='scale');\"> ";
		else{
			echo "\t<tr ";
				echo 'style="background-image:url(images/map_';
				if($this->main->user_id==$user && !$this->no_u) 
					echo sprintf("0%d", $gala)."_o"; 
				else
					echo sprintf("0%d", $gala); 
				echo '.png); filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'images/map_';
				if($this->main->user_id==$user && !$this->no_u) 
					echo sprintf("0%d", $gala)."_o"; 
				else
					echo sprintf("0%d", $gala); 
				echo '.png\', sizingMethod=\'scale\');"';
	
			echo ">\n";
		}

		foreach($this->frow as $key=>$value){
			echo "\t\t<td align=\"center\">";				
				if(isset($this->jump[$key]))
					echo "<a href=\"".url_show($this->jump[$key])."&planet_id=".$this->sql->row['planet_id_s']."&user_id=".$this->sql->row['user_id_s']."\">";
					
					if(preg_match("! AS !", $value))
						if(is_numeric($this->sql->row[$key]) || (strip_tags($this->sql->row[$key])!=$this->sql->row[$key] && is_numeric(strip_tags($this->sql->row[$key]))))
							echo str_replace(strip_tags($this->sql->row[$key]),number_format(strip_tags($this->sql->row[$key]),0,",","."),$this->sql->row[$key]);
						else
							echo $this->sql->row[$key];
					else
						if(is_numeric($this->sql->row[$value]) || (strip_tags($this->sql->row[$value])!=$this->sql->row[$value] && is_numeric(strip_tags($this->sql->row[$value]))))
							str_replace(strip_tags($this->sql->row[$value]),number_format(strip_tags($this->sql->row[$value]),0,",","."),$this->sql->row[$value]);
						else
							echo $this->sql->row[$value];
						
				if(isset($this->jump[$key]))
					echo "</a>";
			echo "</td>\n";
		}
		
		if(sizeof($this->links)>0 or sizeof($this->srow)>0){
			echo "\t\t<td align=\"center\">";
				if(count($this->srow)>0)
					echo "<img id=\"id_{$count}_0\" src=\"images/icon_big.png\" onclick=\"hide('id_{$count}');hide('id_{$count}_0');hide('id_{$count}_1');\" width=\"16\" height=\"16\" /><img id=\"id_{$count}_1\" onclick=\"hide('id_{$count}');hide('id_{$count}_0');hide('id_{$count}_1');\" src=\"images/icon_small.png\" width=\"16\" height=\"16\" style=\"display:none;\" />&nbsp;&nbsp;&nbsp;";			
				
				foreach($this->links as $key=>$value){
					echo "<a href=\"?com={$value}";
					if($this->sql->row['planet_id_s']>0)
						echo "&planet_id=".$this->sql->row['planet_id_s'];
					echo "&user_id=".$this->sql->row['user_id_s']."&ally_id=".$this->sql->row['ally_id_s']."\"<img alt=\"$key\" src=\"images/$key.png\"></a>";	
				}
			echo "</td>\n";
		}
		echo "\t</tr>\n";
	}
	
	function built_srow($count){
		echo "\t<tr id=\"id_$count\" style=\"display:none; background-image:url(images/list_gray.png); filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/list_gray.png', sizingMethod='scale');\">";
		echo "\n\t\t<td colspan=\"";
		
		if(sizeof($this->links)>0)
			echo count($this->frow)-1;
		else
			echo count($this->frow);
			
		echo "\">";
		
			echo "\t\t<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">\n";
			
			$i_c=0;
			$n_r=4;
			if(count($this->srow)<=4)
				$n_r=2;
			elseif(count($this->srow)>4)
				$n_r=3;
			$c=false;
			$e_r=0;
			foreach($this->srow as $key=>$value){
				if($i_c%$n_r==0){
					echo "\t\t\t<tr>";
					$c=true;
					$i_c++;
				}else
					$i_c++;
				
				
				echo "\t\t\t\t<td><strong>";
					if(substr($key,0,1)=="_")
						echo "&nbsp;";
					else
						echo $key;
				echo "</strong></td>\n";
				
				echo "\t\t\t\t<td>";
					if(preg_match("! AS !", $value))
						echo $this->sql->row[$key];
					else
						echo $this->sql->row[$value];
				echo "</td>\n";
				
				$e_r++;
				if($i_c%$n_r==0 && $c){
					echo "\t\t\t</tr>\n";
					$c=false;
					$e_r=0;
				}
			}
			if($e_r!=0)	for ($i = 0; $i <= $n_r-$e_r; $i++) echo "\t\t<td>&nbsp;</td><td>&nbsp;</td>\n";
			
			if($c) echo "\t\t\t</tr>\n";
			
			
			echo "\t\t</table>\n";	
		echo "\t\t</td>\n";
		echo "\t</tr>\n";
	}
}

function url_show($url){	
	if(is_array($url)){
		$link="?";
		foreach($url as $key=>$value){
			if($link!="?") $link.="?";
			$link.="$key=$value";
		}
		return $link;
	}elseif(is_numeric($url)){
		return "?com=$url";
	}else{
		return "http://$url";
	}
}

?>